package com.bmadmin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bmadmin.domain.LayUiDataTablePOJO;
import com.bmadmin.domain.MessageDomain;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.PluginZtreeDataPOJO;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.mapper.BatchSendDao;
import com.bmadmin.mapper.SingleSendDao;
import com.bmadmin.service.BatchSendService;
@Service
@Transactional
public class BatchSendServiceImpl implements BatchSendService {
	@Autowired
	BatchSendDao batchSendDao;
	
	@Override
	public NormalResult getAddressTree(){
		NormalResult result = new NormalResult();
		List<PluginZtreeDataPOJO> addressForZtree = batchSendDao.getAddressTree();
		
        for(PluginZtreeDataPOJO pluginZtreeDataPOJOItemOne:addressForZtree){
            List<PluginZtreeDataPOJO> forAddChildren = new ArrayList<PluginZtreeDataPOJO>();

            for(PluginZtreeDataPOJO pluginZtreeDataPOJOItemTwo:addressForZtree){
                if(pluginZtreeDataPOJOItemOne.getId().equals(pluginZtreeDataPOJOItemTwo.getPart_id())){
                    forAddChildren.add(pluginZtreeDataPOJOItemTwo);
                }
            }
            pluginZtreeDataPOJOItemOne.setChildren(forAddChildren);
        }

        List<PluginZtreeDataPOJO> resultForLevelOne = new ArrayList<PluginZtreeDataPOJO>();

        for(PluginZtreeDataPOJO pluginZtreeDataPOJOItem:addressForZtree){
            if("00".equals(pluginZtreeDataPOJOItem.getPart_id())){
                resultForLevelOne.add(pluginZtreeDataPOJOItem);
            }
        }

        result.setCode("0000");
        result.setMsg("success");
        result.setObject(resultForLevelOne);

        return result;
	}
	
	@Override
	public void InsertMessageBefore(MessageDomain message){
		batchSendDao.InsertMessageBefore(message);
	}
	
	@Override
	public NormalResult deleteBeforeMessage(String s_id){
        NormalResult result = new NormalResult();
        int deleteResult = batchSendDao.deleteBeforeMessage(s_id);
        if(deleteResult==1){
            result.setCode("0000");
            result.setMsg("删除成功");
        }else{
            result.setCode("9999");
            result.setMsg("删除失败");
        }
        return result;
	}
	
	@Override
	public List<MessageDomain> getBeforeMessageList(MessageDomain message){
		return batchSendDao.find(message);
	}
	
	@Override
	public LayUiDataTablePOJO getBeforeMessage(MessageDomain message){
		LayUiDataTablePOJO result = new LayUiDataTablePOJO();

        List<MessageDomain> messageDomain = batchSendDao.find(message);
        result.setCode(0);
        result.setMsg("查询成功");
        result.setData(messageDomain);
        result.setCount(messageDomain.size());


        return result;
	}
	
	@Override
	public List<MessageDomain> findAllBeforeMessage(String s_create_id){
		MessageDomain message = new MessageDomain();
		message.setS_create_id(s_create_id);
		return batchSendDao.find(message);
	}
	
	@Override
	public SysUsersPOJO getUserInfo(String id){
		SysUsersPOJO user = new SysUsersPOJO();
		user.setU_id(id);
		List<SysUsersPOJO> userlist = batchSendDao.getUserInfo(user);
		if(userlist.size()>0){
			return userlist.get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public void updateBeforeMessageBz(String s_id, String content){
		batchSendDao.updateBeforeMessageBz(s_id,content);
	}
}
